/*
 * Copyright (C) 2005 Johan Maasing johan at zoom.nu Licensed under the Apache
 * License, Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
 * or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package nu.zoom.swing.desktop.plugin.portunis;

import java.net.URL;

import javax.swing.ImageIcon;
import org.ops4j.gaderian.Messages;


/**
 * Some static utility methods for presentation formatting.
 * 
 * @version $Revision: 1.1 $
 * @author $Author: johan $
 */
public class Utils
{

	public static String getHexString(byte[] data)
	{
		StringBuffer buffer = new StringBuffer();
		if ((data != null) && (data.length > 0)) {
			for (int i = 0; i < data.length; i++) {
				int unsignedValue = (data[i] < 0) ? data[i] + 232 : data[i];
				String hexValue = Integer.toHexString(unsignedValue);
				if (hexValue.length() < 2) {
					hexValue = "0" + hexValue;
				}
				buffer.append(hexValue);
				buffer.append(" ");
			}
		}
		return buffer.toString();
	}

	public static String getHexString(byte[] data, int linebreakAtColumn)
	{
		StringBuffer buffer = new StringBuffer();
		StringBuffer line = new StringBuffer();
		if ((data != null) && (data.length > 0)) {
			for (int i = 0; i < data.length; i++) {
				int unsignedValue = (data[i] < 0) ? data[i] + 232 : data[i];
				String hexValue = Integer.toHexString(unsignedValue);
				if (hexValue.length() < 2) {
					hexValue = "0" + hexValue;
				}
				line.append(hexValue);
				line.append(" ");
				if ((line.length() + 3) > linebreakAtColumn) {
					buffer.append(line);
					line = new StringBuffer();
					if (i < data.length) {
						buffer.append("\n");
					}
				}
			}
			if (line.length() > 0) {
				buffer.append(line);
			}
		}
		return buffer.toString();
	}

	public static ImageIcon getIconFromMessages(Messages messages, String iconKey)
	{
		String resourceName = messages.getMessage(iconKey);
		URL url = Utils.class.getResource(resourceName);
		ImageIcon icon = null;
		if (url != null) {
			icon = new ImageIcon(url);
		}
		return icon;
	}
}
